package gui;

import javax.swing.JPanel;

import Entities.UserEntity;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
/**
 * 
 * @author Idan Mann
 * This GUI Class presents a simple Chairman access level switch mechanism between two users
 *
 */
public class SwitchChairmanPanel  extends JPanel implements ChatIF
{
	private JTextField newChairmanField;
	private JTextField oldChairmantextField;
	private AcademicFrame frame;
	private ManageEmployeesPanel previousPanel;
	private UserEntity oldChairman;
	private UserEntity newChairman;
	private JButton btnCancel;
	private JButton btnConfirm;
	
	public SwitchChairmanPanel(AcademicFrame frame, ManageEmployeesPanel previousPanel, UserEntity oldChairman, UserEntity newChairman )
	{
		this.frame = frame;
		this.previousPanel = previousPanel;
		this.oldChairman = oldChairman;
		this.newChairman = newChairman;
		
		initialize();
		oldChairmantextField.setText(oldChairman.getFirstName() + " " + oldChairman.getLastName());
		newChairmanField.setText(newChairman.getFirstName() + " " + newChairman.getLastName());
		
		(new Dialog("You have chosen to replace the current chairman, please confirm.")).setVisible(true);
	}
	
	private void initialize()
	{
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Switch Chairman");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNewLabel.setBounds(130, 32, 145, 20);
		add(lblNewLabel);
		
		btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				btnConfirm.setEnabled(false);
				btnCancel.setEnabled(false);
				ArrayList<Object> switchChairman = new ArrayList<Object>();
				switchChairman.add("switchChairman");
				switchChairman.add(oldChairman);
				switchChairman.add(newChairman);
				AcademicFrame.myclient.sendMessageToServer(switchChairman);
			}
		});
		btnConfirm.setBounds(248, 183, 91, 23);
		add(btnConfirm);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				(new Dialog("No changes were saved.")).setVisible(true);
				frame.setContentPane(previousPanel);
				frame.setSize(650,500);
			}
		});
		btnCancel.setBounds(76, 183, 91, 23);
		add(btnCancel);
		
		JLabel lblNewChairman = new JLabel("New Chairman");
		lblNewChairman.setBounds(76, 83, 91, 14);
		add(lblNewChairman);
		
		newChairmanField = new JTextField();
		newChairmanField.setEditable(false);
		newChairmanField.setBounds(177, 80, 162, 20);
		add(newChairmanField);
		newChairmanField.setColumns(10);
		
		JLabel lblReplaces = new JLabel("Replaces");
		lblReplaces.setBounds(76, 131, 80, 14);
		add(lblReplaces);
		
		oldChairmantextField = new JTextField();
		oldChairmantextField.setEnabled(false);
		oldChairmantextField.setBounds(177, 128, 162, 20);
		add(oldChairmantextField);
		oldChairmantextField.setColumns(10);
	}

	@Override
	public void display(Object message)
	{
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("chairmanSwitched")	)
		{
			if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
			{
				ArrayList<Object> setUserAccess = new ArrayList<Object>();
				setUserAccess.add("setUserAccess");
				setUserAccess.add(newChairman);
				AcademicFrame.myclient.sendMessageToServer(setUserAccess);
			}
			else
			{
				(new Dialog((String)(answer.get(1)))).setVisible(true);
			}
		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("userAccessWasSet")	)
			{

				if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
				{
					(new Dialog("User access level was updated.")).setVisible(true);
					frame.setContentPane(previousPanel);
					frame.setSize(650,500);
				}
				else
				{
					(new Dialog((String)(answer.get(1)))).setVisible(true);
					frame.setContentPane(previousPanel);
					frame.setSize(650,500);
				}
			}
		
		
	}

}
